package token

import (
	"github.com/xinliangnote/go-gin-api/internal/code"
	"github.com/xinliangnote/go-gin-api/internal/pkg/core"
	"github.com/xinliangnote/go-gin-api/internal/services/token"
	"net/http"
)

type RequestData struct {
	PageIndex uint   `form:"pageIndex"` // 第几页
	PageSize  uint   `form:"pageSize"`  // 每页显示条数
	ChainName string `form:"chainName"`
	Symbol    string `form:"symbol"`
	Address   string `form:"address"`
	ChainId   uint   `form:"chainId"`
}

func (h *handler) TokenList() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(RequestData)
		res := new(createResponse)

		if err := ctx.ShouldBindQuery(&req); err != nil {

			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				code.Text(code.ParamBindError)).WithError(err),
			)
			return
		}

		data := new(token.RequestServiceData)
		data.Address = req.Address
		data.Symbol = req.Symbol
		data.ChainName = req.ChainName
		data.PageIndex = req.PageIndex
		data.PageSize = req.PageSize
		data.ChainId = req.ChainId

		productList, err := h.tokenService.TokenList(ctx, data)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.GetApplyTokenError,
				code.Text(code.GetApplyTokenError)).WithError(err),
			)
			return
		}

		count, err := h.tokenService.TokenCount(ctx)

		page := new(Page)
		page.Total = count
		page.List = productList

		res.Code = code.StatusOK
		res.Data = page
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}
